<template>
  <div class="cont">
    <el-row>
      <el-col :xs="{span:0,offset:0}" :sm="{span:16,offset:0}">
        <div class="l_c">
          <img src="../../assets/images/show.jpg" alt="展示图" />
        </div>
      </el-col>
      <el-col :xs="{span:22,offset:1}" :sm="{span:7,offset:1}">
        <div class="login">
          <div class="title">用户登录</div>
          <div class="form">
            <div class="item">
              <el-input
                prefix-icon="el-icon-user"
                size="large"
                placeholder="请输入用户名"
                v-model="user.username"
                onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
              ></el-input>
            </div>
            <div class="item">
              <el-input
                prefix-icon="el-icon-lock"
                size="large"
                placeholder="请输入密码"
                v-model="user.password"
                show-password
                onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
              ></el-input>
            </div>
            <div style="height:14px">
              <p class="errtext">{{errtext}}</p>
            </div>
            <div class="item">
              <el-button type="primary" class="loginBtn" @click="goManage">登录</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>


<script>
export default {
  data() {
    return {
      user: { username: "", password: "" },
      errtext: ""
    };
  },
  created() {
    //开启监听键盘按下事件
    window.addEventListener("keydown", this.handleKeyDown, true); 
  },
  methods: {
    //回车键登录
    handleKeyDown() {
      let key = null;
      if (window.event === undefined) {
        key = e.keyCode;
      } else {
        key = window.event.keyCode;
      }
      if (key === 13) {
        this.goManage();
      }
    },
    goManage() {
      if (this.user.username!= "" && this.user.password!= "") {
        this.$axios.post("/api/login", this.user).then(res => {
          if (res.data.message == "Ok" && res.data.code == 200) {
            this.$store.commit("login", res.data.data);
            let newTime = new Date().getTime();
            localStorage.setItem('oldTime', JSON.stringify(newTime));
            this.errtext = "";
            this.user.username = "";
            this.user.password = "";
            this.$router.push({ path: "/manage" });
          } else {
            this.errtext = "用户名或密码错误!";
          }
        });
      } else {
        this.errtext = "账号和密码不能为空!";
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.cont {
  max-width: 1000px;
  margin: 20px auto;
  .l_c img {
    border-radius: 8px;
    box-shadow: 0 0 8px black;
    height: 410px;
  }
  .login {
    height: 400px;
    margin-top: 6px;
    border: 1px solid #0075a9;
    .title {
      border-bottom: 1px solid #0075a9;
      height: 50px;
      font-size: 24px;
      text-align: center;
      line-height: 50px;
      color: #0b4f6d;
    }
    .form {
      margin-top: 20px;
      padding: 20px;
      text-align: center;
      .errtext {
        text-align: left;
        padding-left: 6px;
        height: 100%;
        color: #f56c6c;
        font-size: 12px;
      }
      .item {
        margin-top: 20px;
        margin-bottom: 10px;
        /deep/ .el-input__inner {
          height: 50px;
          line-height: 50px;
          border: 1px solid #0075a9;
          font-size: 16px;
          padding-left: 34px;
        }
        /deep/ .el-input__prefix {
          color: #0075a9;
          font-size: 20px;
          top: 2px;
        }
        .loginBtn {
          width: 100%;
          height: 50px;
          background-color: #0075a9;
          font-size: 20px;
          font-family: "宋体";
        }
      }
    }
  }
}
</style>